package org.dkd.controller;


import com.jfoenix.controls.JFXButton;
import de.felixroske.jfxsupport.FXMLController;
import io.datafx.controller.ViewController;
import io.datafx.controller.flow.context.FXMLViewFlowContext;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import org.dkd.helper.Refreshable;
import org.dkd.rest.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.kafka.core.KafkaTemplate;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;

@ViewController(value = "/fxml/test1.fxml", title = "日志查看")
public class TestTableViewController extends Refreshable {

    @Autowired
    private TestService testService;

    @FXML
    private JFXButton testButton;

    @FXMLViewFlowContext
    private ViewFlowContext context;

    private VBox labelContext;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @PostConstruct
    public void init() throws Exception {
        register();
        testButton.setOnAction(event -> {
            kafkaTemplate.send("test.abc.topic", "hello", "hello:" + System.currentTimeMillis());
        });
    }

    public void showMessage(String message) {
        String[] arr = message.split(":");
        String timestamp = arr[1];
        long delay = System.currentTimeMillis() / 1000 - Long.parseLong(timestamp) / 1000;
        labelContext.getChildren().add(new Label(message + "    delay:" + delay + "s"));
    }
}
